<?php

namespace App\Http\Controllers\Client\Shop\Explore;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class ExploreListController extends ClientController
{

    public function index()
    {

        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 10;
        $pageNo = $all['pageNo'] ?? 1;

        try {
            // 查询当前账号接单过的shop_id（取消后可以重接）
            $shop_ids = DB::table('explore_work')
                ->where('receiver_we_id', '=', $this->we_id)
                ->where('status', '<>', Data::DATA_DB_EXPLORE_WORK_STATUS_ERROR)
                ->pluck('shop_id')
                ->toArray();

            // 查询当前账号等级
            $channel_id = DB::table('talent')
                ->where('we_id', '=', $this->we_id)
                ->where('status', '=', 1)
                ->value('channel_id');
            $channel_id = is_null($channel_id) ? 0 : $channel_id;

            $shops = DB::table('explore_order as eo')
                ->leftJoin('shop', 'eo.shop_id', '=', 'shop.shop_id')
                ->leftJoin('dict_area as da', 'shop.district', '=', 'da.id')
                ->leftJoin('explore_channel as ec', 'eo.channel_id', '=', 'ec.channel_id')
                ->leftJoin('lottery_users as lu', 'eo.we_id', '=', 'lu.we_id')
                ->where('eo.status', '=', Data::DATA_DB_EXPLORE_ORDER_STATUS_PENDING) // 正在派单中的订单
                ->where(function ($query) use ($all) {
                    if ($all['channel_id'] > 0) {
                        $query->where('eo.channel_id', '=', $all['channel_id']); // 筛选等级
                    }
                })
                ->orderByDesc('eo.created_at')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->select('eo.eo_id', 'eo.we_id', 'eo.shop_id', 'eo.channel_id', 'eo.start_date', 'eo.end_date',
                    'eo.total_num', 'eo.remaining_num', 'eo.remark', 'eo.offline', 'eo.task_we_id', 'shop.shop_type','shop.head_img','shop.key_word',
                    'shop.shop_name', 'da.id as city', 'da.short_name', 'shop.address', 'shop.platform',
                    'ec.channel_name', 'ec.price')
                ->get()
                ->map(function ($item) use ($channel_id, $shop_ids) {
                    $item->shop_type_name = Data::$laifeiShopTypes[$item->shop_type];
                    $item->shop_name = '****' . mb_substr($item->shop_name,4);
                    $item->reject_reason = '';
                    $item->tasking = $item->task_we_id === $this->we_id;
                    $item->ttl = Redis::ttl('explore_order_task_' . $item->eo_id) ?? false;
                    $item->head_img = AliOss::getUrl($item->head_img)?? null;

                    $item->permit = false;
                    if($item->we_id === $this->we_id){
                        $item->reject_reason = '您自己的任务';
                    } elseif(in_array($item->shop_id, $shop_ids)){
                        $item->reject_reason = '您已接单过此店铺';
                    } elseif($item->channel_id > $channel_id){
                        $item->reject_reason = '您的达人等级不符合商家要求';
                    } else {
                        $item->permit = true;
                    }

                    return $item;
                })
                ->toArray();

            return $this->setReturnJson(0, 'success', $shops);
        } catch (\Exception $e) {
            Log::error(ExploreListController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败');
        }

    }

}
